<?php

/**
 * This file is part of Switcher.
 * 
 * Switcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Switcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Switcher. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace switcher\log;

/**
 * The <b>SilentLogger</b> class is both a logger factory and a logger implementation. It is
 * used by the <i>Switcher</i> framework to provide integration to native PHP logging.
 * 
 * @author Dri <switcher.framework@gmail.com>
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 * @package switcher\log
 * @since 1.0.0
 */
final class SystemLogger extends \switcher\log\LoggerFactory implements \switcher\log\Logger
{
	/**
	 * Indicates if a log connection is open.
	 * @var bool
	 */
	private $connectionOpened;
	
	/**
	 * Open a connection to the system logger.
	 */
	protected function __construct()
	{
		$connectionOpened = @openlog("[Switcher]", LOG_ODELAY, LOG_USER);
	}
	
	/**
	 * Open the connection to the system logger.
	 */
	protected function __destruct()
	{
		if ($connectionOpened)
		{
			@closelog();
		}
	}
	
	/**
	 * Log a message at the specified level.
	 * 
	 * @param integer $level The log level
	 * @param string $message The message to be logged
	 */
	private function log($level, $message)
	{
		if ($connectionOpened)
		{
			@syslog($level, $message);
		}
	}
	
	/**
	 * Log a message at the INFO level.
	 * 
	 * @param $message The message to be logged
	 */
	public function info($message)
	{
		$this->log(LOG_INFO, $message);
	}
	
	/**
	 * Log a message at the WARNING level.
	 * 
	 * @param $message The message to be logged
	 */
	public function warning($message)
	{
		$this->log(LOG_WARNING, $message);
	}
	
	/**
	 * Log a message at the ERROR level.
	 * 
	 * @param $message The message to be logged
	 */
	public function error($message)
	{
		$this->log(LOG_ERR, $message);
	}
	
	/**
	 * Log a message at the FATAL level.
	 * 
	 * @param $message The message to be logged
	 */
	public function fatal($message)
	{
		$this->log(LOG_CRIT, $message);
	}
	
	/**
	 * Returns itself to ensure one single instance of the class.
	 * 
	 * @return Logger itself
	 */
	public function getLogger($class)
	{
		return $this;
	}
}

?>